package thread;

public class Demo15 {
    //count操作不是原子的，分为三步，把读取内存的值，count++，把值放在内存里，
    //sychronized是锁操作
    private static int count;
    public static void main(String[] args) throws InterruptedException{
            Object locker=new Object();
            Thread t1=new Thread(()->{ for (int i = 0; i <50000 ; i++) {
                synchronized(locker){
                    count++;
                }
            }});
            Thread t2=new Thread(()->{
                for (int i = 0; i <50000; i++) {
                    synchronized(locker){
                        count++;
                    }
                }
            });
            t1.start();
            t2.start();
            //主线程等待t1线程；
            t1.join();
            t2.join();
        System.out.println("main线程结束"+count);

    }
}
